package com.example.admin.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.admin.mapper.BlogMapper;
import com.example.admin.mapper.CategoryMapper;
import com.example.admin.service.BlogService;
import com.example.xo.pojo.Blog;
import com.example.xo.pojo.Category;
import com.example.xo.vo.BlogVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author YangJiaHao
 */
@Service
@Slf4j
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public boolean add(Blog blog) {
        //生成雪花id
        Snowflake snowflake = IdUtil.getSnowflake();
        String id = snowflake.nextIdStr();

        Blog blogInDb = findBlogByTitle(blog.getTitle());
        if (blogInDb != null) {
            log.info("标题{}重复", blog.getTitle());
            return false;
        }
        //新增时间 数据库设计阶段我们有指定当新增时间是null的时候，采用当前时间
        //blog.setCreated(LocalDateTime.now());
        int res = blogMapper.insert(blog);
        return res == 1;
    }

    @Override
    public boolean delete(String id) {
        //软删除
        Blog blog = findBlogById(id);

        if (blog == null) {
            log.info("输入的id：{}不存在", id);
            return false;
        }
        blog.setDeleted(LocalDateTime.now());

        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId,id);

        int res = blogMapper.update(blog,wrapper);
        log.info("受影响的行数:{}", res);
        return res == 1;
    }

    @Override
    public boolean update(Blog blog) {
        Blog blogInDb = findBlogByTitle(blog.getTitle());
        //判断是否为null  以及判断id是否相同 相同才能进行修改
        if (blogInDb != null && !blog.getBlogId().equals(blogInDb.getBlogId())) {
            log.info("标题名{}重复", blog.getTitle());
            return false;
        }

        blog.setUpdated(LocalDateTime.now());

        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId, blog.getBlogId());
        wrapper.isNull(Blog::getDeleted);

        //去掉对作者的修改
        blog.setAuthor(null);

        int res = blogMapper.update(blog, wrapper);
        log.info("受影响的行数:{}", res);
        return res > 0;
    }

    @Override
    public List<BlogVO> findAll() {
        List<BlogVO> vos = new ArrayList<>();

        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.orderByDesc(Blog::getCreated);
        List<Blog> blogs = blogMapper.selectList(wrapper);
        //加强
        if (blogs == null) {
            log.info("无数据");
            return null;
        }
        for (Blog blog : blogs) {
            //根据blog的categoryId查询分类对象
            Category category = categoryMapper.selectById(blog.getCategoryId());
            //封装数据
            BlogVO vo = new BlogVO();
            vo.setBlogId(blog.getBlogId());
            vo.setCategoryName(category.getCategoryName());
            vo.setTitle(blog.getTitle());
            vo.setAuthor(blog.getAuthor());
            vo.setCreated(blog.getCreated());

            vos.add(vo);
        }
        return vos;
    }

    @Override
    public BlogVO findById(String id) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId, id);
        wrapper.isNull(Blog::getDeleted);
        Blog blog = blogMapper.selectOne(wrapper);

        //加强
        if (blog == null) {
            log.info("输入的id：{}不存在", id);
            return null;
        }

        //根据blog的categoryId查询分类对象
        Category category = categoryMapper.selectById(blog.getCategoryId());

        BlogVO vo = new BlogVO();
        vo.setBlogId(blog.getBlogId());
        vo.setCategoryName(category.getCategoryName());
        vo.setTitle(blog.getTitle());
        vo.setAuthor(blog.getAuthor());
        vo.setCreated(blog.getCreated());

        return vo;
    }

    @Override
    public BlogVO findByTitle(String title) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getTitle, title);
        wrapper.isNull(Blog::getDeleted);
        Blog blog = blogMapper.selectOne(wrapper);

        //根据blog的categoryId查询分类对象
        Category category = categoryMapper.selectById(blog.getCategoryId());

        BlogVO vo = new BlogVO();
        vo.setBlogId(blog.getBlogId());
        vo.setCategoryName(category.getCategoryName());
        vo.setTitle(blog.getTitle());
        vo.setAuthor(blog.getAuthor());
        vo.setCreated(blog.getCreated());

        return vo;
    }

    @Override
    public Blog findBlogByTitle(String title) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getTitle, title);
        wrapper.isNull(Blog::getDeleted);
        Blog blog = blogMapper.selectOne(wrapper);

        return blog;
    }

    public Blog findBlogById(String id) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getBlogId, id);
        wrapper.isNull(Blog::getDeleted);
        Blog blog = blogMapper.selectOne(wrapper);

        //加强
        if (blog == null) {
            log.info("输入的id：{}不存在", id);
            return null;
        }

        return blog;
    }

    @Override
    public Blog findPoById(String id) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(Blog::getDeleted);
        wrapper.eq(Blog::getBlogId,id);
        Blog blog = blogMapper.selectOne(wrapper);
        return blog;
    }
}
